from sqlalchemy import Column, Integer, String, Boolean, DateTime, func
from sqlalchemy.orm import relationship
from database import Base
from model.cname_node import cname_node
from model.cdn_config_node import cdn_config_node

class CdnNode(Base):
    __tablename__ = "t_cdn_node"

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    name = Column(String(100), nullable=False, comment="节点名称")
    host = Column(String(255), unique=True, nullable=False, comment="节点IP或域名")
    ssh_user = Column(String(50), default="root", comment="SSH登录用户名")
    ssh_port = Column(Integer, default=22, comment="SSH端口")
    status = Column(Boolean, default=True, comment="是否启用/可用")
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="更新时间")

    configs = relationship(
        "CdnConfig",
        secondary=cdn_config_node,
        back_populates="nodes"
    )

    cnames = relationship(
        "CName",
        secondary=cname_node,
        back_populates="nodes"
    )
